package com.tianzeculture.stagebao.activity.home.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.adapter.PrefessionRecyAdapter;
import com.tianzeculture.stagebao.activity.artist.adapter.PrefessionRightAdapter;
import com.tianzeculture.stagebao.bean.AllIndustryBean;
import com.tianzeculture.stagebao.bean.ListpBean;
import com.tianzeculture.stagebao.view.MyRecyclerview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 招募类型
 */

public class PrefessionTypePopWindow extends PopupWindow {
    private Activity mContext;
    private final LayoutInflater inflater;
    private View styleView;
    private final Display display;
    private final WindowManager windowManager;
    private NoticeTypeListener noticeTypeListener;
    private List<String> list = new ArrayList<>();
    RecyclerView leftRecyclerview;
    RecyclerView rightRecyclerview;

    PrefessionRightAdapter adapter2;
    List<AllIndustryBean> allIndustryBeanList;

    public PrefessionTypePopWindow(Activity context, final List<AllIndustryBean> allIndustryBeanList) {
        this.mContext = context;
        this.allIndustryBeanList = allIndustryBeanList;
        list.add("不限");
        for (AllIndustryBean bean : allIndustryBeanList) {
            list.add(bean.getTag());
        }
        list.add("其他");
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        styleView = inflater.inflate(R.layout.popwindow_prefession_type, null);

        this.setContentView(styleView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));

        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);


        leftRecyclerview = styleView.findViewById(R.id.leftRecyclerview);
        rightRecyclerview = styleView.findViewById(R.id.rightRecyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        leftRecyclerview.setLayoutManager(layoutManager);
        leftRecyclerview.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rightRecyclerview.setLayoutManager(manager);
        rightRecyclerview.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));

        PrefessionRecyAdapter adapter1 = new PrefessionRecyAdapter(R.layout.item_job_two, list);
        leftRecyclerview.setAdapter(adapter1);
        adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final String tag = list.get(position);
                if (position == 0 || position == allIndustryBeanList.size() + 1) {
                    noticeTypeListener.pickNoticeTypeValue(list.get(position));
                    dismiss();
                    return;
                }
                rightRecyclerview.setVisibility(View.VISIBLE);
                final List<String> stringList = new ArrayList<String>();
                stringList.clear();
                stringList.add("不限");
                stringList.addAll(allIndustryBeanList.get(position - 1).getList());
                stringList.add("其他");
                adapter2 = new PrefessionRightAdapter(R.layout.item_job_two, stringList);
                rightRecyclerview.setAdapter(adapter2);
                adapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        String str = stringList.get(position);
                        if ("不限".equals(str))
                            str = tag;
                        noticeTypeListener.pickNoticeTypeValue(str);
                        dismiss();
                    }
                });
            }
        });
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


    public interface NoticeTypeListener {
        void pickNoticeTypeValue(String value);

        void noticeTypeCancel();
    }

    public void setNoticeTypeListener(NoticeTypeListener listener) {
        this.noticeTypeListener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        noticeTypeListener.noticeTypeCancel();
    }
}
